//
//  VideoToolBar.m
//  视频播放
//
//  Created by zhangzhenyun on 2018/8/1.
//  Copyright © 2018年 Beijing Youjiu Network. All rights reserved.
//

#import "ZYVideoToolBar.h"

@interface ZYVideoToolBar ()
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIButton *playPauseBtn;
@property (nonatomic, strong) UILabel *playTimeLab;
@property (nonatomic, strong) ZYVideoSlider *slider;
@property (nonatomic, strong) UILabel *totalTimeLab;

@end

@implementation ZYVideoToolBar
- (instancetype)init {
    if (self = [super init]) {
        [self setupViews];
    }
    return self;
}
- (void)setupViews {
    [self addSubview:self.backView];
    [self addSubview:self.playPauseBtn];
    [self addSubview:self.playTimeLab];
    [self addSubview:self.slider];
    [self addSubview:self.totalTimeLab];
    [self setupLayout];
}
#pragma mark - target
- (void)playOrPauseClick:(UIButton *)btn {
    if (self.playOrPauseBlock) {
        self.playOrPauseBlock(btn);
    }
}
- (void)sliderValueChange:(ZYVideoSlider *)slider {
    if (self.valueChangeBlock) {
        self.valueChangeBlock(slider);
    }
}
- (void)finishChange {
    if (self.finishChangeBlock) {
        self.finishChangeBlock(self.slider);
    }
}
- (void)dragSlider {
    if (self.draggingSliderBlock) {
        self.draggingSliderBlock(self.slider);
    }
}
#pragma mark - 布局
- (void)setupLayout {
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.playPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView).offset(Fix375(4));
        make.bottom.equalTo(self.backView.mas_bottom).offset(-Fix375(8));
        make.size.mas_equalTo(CGSizeMake(Fix375(39), Fix375(39)));
    }];
    [self.playTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.playPauseBtn.mas_right).offset(Fix375(5));
        make.centerY.equalTo(self.playPauseBtn);
    }];
    [self.totalTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backView.mas_right).offset(-Fix375(20));
        make.centerY.equalTo(self.playPauseBtn);
    }];
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.playTimeLab.mas_right).offset(Fix375(10));
        make.right.equalTo(self.totalTimeLab.mas_left).offset(-Fix375(10));
        make.top.equalTo(self.backView);
        make.bottom.equalTo(self.backView);
    }];
}
#pragma mark - getter
-(UIView *)backView {
    if (_backView == nil) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor redColor];
    }
    return _backView;
}
-(UIButton *)playPauseBtn {
    if (_playPauseBtn == nil) {
        _playPauseBtn = [[UIButton alloc] init];
        [_playPauseBtn setImage:[UIImage imageNamed:@"zanting"] forState:UIControlStateNormal];
        [_playPauseBtn setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateSelected];
        [_playPauseBtn addTarget:self action:@selector(playOrPauseClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playPauseBtn;
}
-(UILabel *)playTimeLab {
    if (_playTimeLab == nil) {
        _playTimeLab = [[UILabel alloc] init];
        _playTimeLab.text = @"00:00";
        _playTimeLab.font = RMFont(10);
        _playTimeLab.textColor = [UIColor whiteColor];
    }
    return _playTimeLab;
}
-(ZYVideoSlider *)slider {
    if (_slider == nil) {
        _slider = [[ZYVideoSlider alloc] init];
        _slider.value = 0.0f;
        _slider.middleValue = 0.0f;
        _slider.minColor = col_3296FA;
        _slider.maxColor = [UIColor whiteColor];
        _slider.middleColor = [UIColor whiteColor];
        __weak typeof(self) weakSelf = self;
        _slider.valueChangeBlock = ^(ZYVideoSlider *slider){
            [weakSelf sliderValueChange:slider];
        };
        _slider.finishChangeBlock = ^(ZYVideoSlider *slider){
            [weakSelf finishChange];
        };
        _slider.draggingSliderBlock = ^(ZYVideoSlider *slider){
            [weakSelf dragSlider];
        };
    }
    return _slider;
}
-(UILabel *)totalTimeLab {
    if (_totalTimeLab == nil) {
        _totalTimeLab = [[UILabel alloc] init];
        _totalTimeLab.text = @"00:00";
        _totalTimeLab.font = RMFont(10);
        _totalTimeLab.textColor = [UIColor whiteColor];
    }
    return _totalTimeLab;
}

@end
